class Solution {
public:
    bool searchMatrix(vector<vector<int> > &matrix, int target) {
        int n=matrix.size();
        if (n==0) return false;
        int m=matrix[0].size();
        if (m==0) return false;
        int h=0, t=n*m-1;
        while (h<=t) {
            int w=(h+t)/2;
            if (matrix[w/m][w%m]==target) return true;
            if (matrix[w/m][w%m]>target) t=w-1; else h=w+1;
        }
        return false;
    }
};
